# -*- coding: utf-8 -*-
"""
@Time: 2021/12/20 14:49
@Author: jins
@File: 时序数据计算写入测试数据.py
@Introduction: 
"""
import random
from datetime import datetime
from common.operateKafka import OperateKafka

tm = datetime(2021, 12, 1, 0, 0)
fmt_s = '%Y%m%d%H%M%S'
efaa_actual_kwh = 2000.00  # A相正向有功电量实际值(千瓦时)
efab_actual_kwh = 1000.00  # B相正向有功电量实际值(千瓦时)
efac_actual_kwh = 500.00  # C相正向有功电量实际值(千瓦时)
efat_actual_kwh = efaa_actual_kwh + efab_actual_kwh + efac_actual_kwh  # 正向有功总电量实际值(千瓦时)
efrt_actual_kvarh = 200.00  # 正向无功总电量实际值(千乏时)
eraa_actual_kwh = 600.00  # A相反向有功电量实际值(千瓦时)
erab_actual_kwh = 300.00  # B相反向有功电量实际值(千瓦时)
erac_actual_kwh = 150.00  # C相反向有功电量实际值(千瓦时)
erat_actual_kwh = eraa_actual_kwh + erab_actual_kwh + erac_actual_kwh  # 反向有功总电量实际值(千瓦时)
errt_actual_kvarh = 40.00  # 反向无功总电量实际值(千乏时)
ds_origin_kw = 200.0  # 正向有功尖需量原始值(千瓦)
dp_origin_kw = 200.0  # 正向有功峰需量原始值(千瓦)
df_origin_kw = 200.0  # 正向有功平需量原始值(千瓦)
do_origin_kw = 200.0  # 正向有功谷需量原始值(千瓦)
dt_origin_kw = ds_origin_kw + dp_origin_kw + df_origin_kw + do_origin_kw  # 正向有功需量原始值(千瓦)
indfaa_origin_kwh = 2000.0  # A相正向有功示值原始值(千瓦时)
indfab_origin_kwh = 1000.0  # B相正向有功示值原始值(千瓦时)
indfac_origin_kwh = 500.0  # C相正向有功示值原始值(千瓦时)
indfat_origin_kwh = indfaa_origin_kwh + indfab_origin_kwh + indfac_origin_kwh  # 正向有功总示值原始值(千瓦时)
indfrt_origin_kvarh = 1000.0  # 正向无功总示值原始值(千乏时)
indraa_origin_kwh = 400.0  # A相反向有功示值原始值(千瓦时)
indrab_origin_kwh = 200.0  # B相反向有功示值原始值(千瓦时)
indrac_origin_kwh = 100.0  # C相反向有功示值原始值(千瓦时)
indrat_origin_kwh = indraa_origin_kwh + indrab_origin_kwh + indrac_origin_kwh  # 反向有功总示值原始值(千瓦时)
indrp1_origin_kvarh = 100.0  # 一象限无功示值原始值(千乏时)
indrp2_origin_kvarh = 80.0  # 二象限无功示值原始值(千乏时)
indrp3_origin_kvarh = 60.0  # 三象限无功示值原始值(千乏时)
indrp4_origin_kvarh = 40.0  # 四象限无功示值原始值(千乏时)
indrrt_origin_kvarh = indrp1_origin_kvarh + indrp2_origin_kvarh + indrp3_origin_kvarh + indrp4_origin_kvarh  # 反向无功总示值原始值(千乏时)
dt_time_empty = str(tm.strftime(fmt_s))  # 正向有功需量发生时间(无单位)
ds_time_empty = str(tm.strftime(fmt_s))  # 正向有功尖需量发生时间(无单位)
dp_time_empty = str(tm.strftime(fmt_s))  # 正向有功峰需量发生时间(无单位)
df_time_empty = str(tm.strftime(fmt_s))  # 正向有功平需量发生时间(无单位)
do_time_empty = str(tm.strftime(fmt_s))  # 正向有功谷需量发生时间(无单位)
ua_origin_v = round(random.uniform(210.0, 230.0), 2)  # A相电压原始值(伏特)
ub_origin_v = round(random.uniform(210.0, 230.0), 2)  # B相电压原始值(伏特)
uc_origin_v = round(random.uniform(210.0, 230.0), 2)  # C相电压原始值(伏特)
ia_origin_a = round(random.uniform(3.0, 4.0), 2)  # A相电流原始值(安培)
ib_origin_a = round(random.uniform(3.0, 4.0), 2)  # B相电流原始值(安培)
ic_origin_a = round(random.uniform(3.0, 4.0), 2)  # C相电流原始值(安培)
pa_actual_kw = round((ua_origin_v * ia_origin_a), 2)  # A相有功功率实际值(千瓦)
pb_actual_kw = round((ub_origin_v * ib_origin_a), 2)  # B相有功功率实际值(千瓦)
pc_actual_kw = round((uc_origin_v * ic_origin_a), 2)  # C相有功功率实际值(千瓦)
pt_actual_kw = pa_actual_kw + pb_actual_kw + pc_actual_kw  # 总有功功率实际值(千瓦)
qa_actual_kvar = round(random.uniform(1.0, 2.0), 2)  # A相无功功率实际值(千乏)
qb_actual_kvar = round(random.uniform(1.0, 2.0), 2)  # B相无功功率实际值(千乏)
qc_actual_kvar = round(random.uniform(1.0, 2.0), 2)  # C相无功功率实际值(千乏)
pfa_origin_empty = round(random.uniform(0, 1.0), 2)  # A相功率因数原始值(无单位)
pfb_origin_empty = round(random.uniform(0, 1.0), 2)  # B相功率因数原始值(无单位)
pfc_origin_empty = round(random.uniform(0, 1.0), 2)  # C相功率因数原始值(无单位)
pft_origin_empty = pfa_origin_empty + pfb_origin_empty + pfc_origin_empty  # 总功率因数原始值(无单位)
qt_actual_kvar = qa_actual_kvar + qb_actual_kvar + qc_actual_kvar  # 总无功功率实际值(千乏)
iz_origin_a = round(random.uniform(3.0, 4.0), 2)  # 零相电流原始值(安培)
uaa_origin_d = round(random.uniform(10.0, 30.0), 2)  # A相电压相位角原始值(度)
uab_origin_d = round(random.uniform(10.0, 30.0), 2)  # B相电压相位角原始值(度)
uac_origin_d = round(random.uniform(10.0, 30.0), 2)  # C相电压相位角原始值(度)
iaa_origin_d = round(random.uniform(5.0, 10.0), 2)  # A相电流相位角原始值(度)
iab_origin_d = round(random.uniform(5.0, 10.0), 2)  # B相电流相位角原始值(度)
iac_origin_d = round(random.uniform(5.0, 10.0), 2)  # C相电流相位角原始值(度)
ta_origin_dc = round(random.uniform(20.0, 40.0), 2)  # A相温度原始值(摄氏度)
tb_origin_dc = round(random.uniform(20.0, 40.0), 2)  # B相温度原始值(摄氏度)
tc_origin_dc = round(random.uniform(20.0, 40.0), 2)  # C相温度原始值(摄氏度)
paa_origin_kw = pa_actual_kw  # A相分钟平均有功功率原始值(千瓦)
pab_origin_kw = pb_actual_kw  # B相分钟平均有功功率原始值(千瓦)
pac_origin_kw = pc_actual_kw  # C相分钟平均有功功率原始值(千瓦)
# = round(random.uniform(2.0, 5.0), 2)  # 电流不平衡度原始值(度)
uub_origin_d = round(random.uniform(2.0, 5.0), 2)  # 电压不平衡度原始值(度)

data_dict = [{'data_time': 1640865216000,
              'efaa_actual_kwh': str(efaa_actual_kwh),
              'efab_actual_kwh': str(efab_actual_kwh),
              'efac_actual_kwh': str(efac_actual_kwh),
              'efat_actual_kwh': str(efat_actual_kwh),
              'efrt_actual_kvarh': str(efrt_actual_kvarh),
              'eraa_actual_kwh': str(eraa_actual_kwh),
              'erab_actual_kwh': str(erab_actual_kwh),
              'erac_actual_kwh': str(erac_actual_kwh),
              'erat_actual_kwh': str(erat_actual_kwh),
              'errt_actual_kvarh': str(errt_actual_kvarh),
              'ds_origin_kw': str(ds_origin_kw),
              'dp_origin_kw': str(dp_origin_kw),
              'df_origin_kw': str(df_origin_kw),
              'do_origin_kw': str(do_origin_kw),
              'dt_origin_kw': str(dt_origin_kw),
              'indfaa_origin_kwh': str(indfaa_origin_kwh),
              'indfab_origin_kwh': str(indfab_origin_kwh),
              'indfac_origin_kwh': str(indfac_origin_kwh),
              'indraa_origin_kwh': str(indraa_origin_kwh),
              'indrab_origin_kwh': str(indrab_origin_kwh),
              'indrac_origin_kwh': str(indrac_origin_kwh),
              'indrp1_origin_kvarh': str(indrp1_origin_kvarh),
              'indrp2_origin_kvarh': str(indrp2_origin_kvarh),
              'indrp3_origin_kvarh': str(indrp3_origin_kvarh),
              'indrp4_origin_kvarh': str(indrp4_origin_kvarh),
              'dt_time_empty': str(dt_time_empty),
              'ds_time_empty': str(ds_time_empty),
              'dp_time_empty': str(dp_time_empty),
              'df_time_empty': str(df_time_empty),
              'do_time_empty': str(do_time_empty),
              'ua_origin_v': str(ua_origin_v),
              'ub_origin_v': str(ub_origin_v),
              'uc_origin_v': str(uc_origin_v),
              'ia_origin_a': str(ia_origin_a),
              'ib_origin_a': str(ib_origin_a),
              'ic_origin_a': str(ic_origin_a),
              'iz_origin_a': str(iz_origin_a),
              'uaa_origin_d': str(uaa_origin_d),
              'uab_origin_d': str(uab_origin_d),
              'uac_origin_d': str(uac_origin_d),
              'iaa_origin_d': str(iaa_origin_d),
              'iab_origin_d': str(iab_origin_d),
              'iac_origin_d': str(iac_origin_d),
              'ta_origin_dc': str(ta_origin_dc),
              'tb_origin_dc': str(tb_origin_dc),
              'tc_origin_dc': str(tc_origin_dc),
              'paa_origin_kw': str(paa_origin_kw),
              'pab_origin_kw': str(pab_origin_kw),
              'pac_origin_kw': str(pac_origin_kw),
              #'iub_origin_d': str(iub_origin_d),
              'uub_origin_d': str(uub_origin_d),
              'indfat_origin_kwh': None,
              'indfrt_origin_kvarh': None,
              'indrat_origin_kwh': None,
              'indrrt_origin_kvarh': None,
              'pa_actual_kw': None,
              'pb_actual_kw': None,
              'pc_actual_kw': None,
              'pt_actual_kw': None,
              'qa_actual_kvar': None,
              'qb_actual_kvar': None,
              'qc_actual_kvar': None,
              'pfa_origin_empty': None,
              'pfb_origin_empty': None,
              'pfc_origin_empty': None,
              'pft_origin_empty': None,
              'qt_actual_kvar': None,
              'iub_origin_d': None
              }]
data = [
    {
        "dataSource": 1,
        "responseDeviceIdsMap": {},
        "responseId": "033119800-1639032302346",
        "responseResults": [
            {
                "businessCode": "device_non_electric_ts",
                "businessType": "device_non_electric",
                "deviceResults": [
                    {
                        "childDeviceCode": "2",
                        "childDeviceId": "121634883809320960",
                        "code": 20000,
                        "data": data_dict,
                        "deviceCode": "033119800",
                        "deviceId": "zszd033119800",
                        "frequency": "mi15",
                        "regionCode": "324090649",
                        "success": True,
                        "sysType": [
                            "es"
                        ]
                    }
                ],
                "schemaCode": "QKL8163_2_2018",
                "serviceIdentifier": "0DH"
            }
        ],
        "responseTime": "2021-12-09 14:45:02",
        "responseType": 2,
        "sysCode": "tcp"
    }
]

if __name__ == '__main__':
    op = OperateKafka(version=221)
    # res = op.read_message('MICHEAL_COMMUNICATION_RESPONSE_DATA')
    # print(res)
    op.send_message('COMMUNICATION_RESPONSE_ANALOG', data)
